package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.service.mongo.UserLikeService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     *  保存用户的喜欢 （第一次访问这个方法  和第N次访问这个方法）
     * @param loginUserId
     * @param userLikeId
     */
    @Override
    public void saveUserLike(Long loginUserId, Long userLikeId) {
        //1.判断我是否喜欢过这个人
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
                        .and("likeUserId").is(userLikeId)
        );
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        //2.如果没有喜欢  添加喜欢的记录
        if (userLike == null) {
            //2.1创建对象
            userLike = new UserLike();
            //2.2封装数据
            userLike.setCreated(System.currentTimeMillis());
            userLike.setUserId(loginUserId);
            userLike.setLikeUserId(userLikeId);
            //2.3调用方法保存
            mongoTemplate.save(userLike);

        }
    }



    /**
     * 判断双方是否互为喜欢
     * @param loginUserId
     * @param userLikeId
     * @return
     */
    @Override
    public boolean isUserLike(Long loginUserId, Long userLikeId) {
        //1.我是否喜欢对方
        Query query1 = new Query(
                Criteria.where("userId").is(loginUserId)
                .and("likeUserId").is(userLikeId)
        );
        boolean flag1 = mongoTemplate.exists(query1, UserLike.class);
        //2.对方是否喜欢我
        Query query2 = new Query(
                Criteria.where("userId").is(userLikeId)
                        .and("likeUserId").is(loginUserId)
        );
        boolean flag2 = mongoTemplate.exists(query2, UserLike.class);
        return flag1&&flag2;
        //return 变量>0;  查看是否有记录
    }

    /**
     * 删除用户的喜欢
     * @param loginUserId
     * @param userDislikeId
     */
    @Override
    public void deleteUserLike(Long loginUserId, Long userDislikeId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
                        .and("likeUserId").is(userDislikeId)
        );

        //删除
        mongoTemplate.remove(query, UserLike.class);

    }

    /**
     * 查询互相喜欢
     * @param loginUserId
     * @return
     */
    @Override
    public Long findEachLoveCount(Long loginUserId) {
        //删除好友的时候   记得是删除双向关系
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
        );
        long count = mongoTemplate.count(query, Friend.class);
        return count;
    }

    /**
     *  查询我的喜欢
     * @param loginUserId
     * @return
     */
    @Override
    public Long findLoveCount(Long loginUserId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
        );

        long count = mongoTemplate.count(query, UserLike.class);
        return count;    }

    /**
     * 查询我的粉丝
     * @param loginUserId
     * @return
     */
    @Override
    public Long findFanCount(Long loginUserId) {
        Query query = new Query(
                Criteria.where("likeUserId").is(loginUserId)
        );
        long count = mongoTemplate.count(query, UserLike.class);
        return count;    }

    /**
     *  查看  互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * //明确分页 明确转换的对象
     * //1.互相喜欢
     * //1.1 查询
     * //1.2 封装vo
     * //1.3 创建pageBeanVo
     * //2.喜欢
     * //2.1 查询
     * //2.2 封装vo
     * //2.3 创建pageBeanVo
     * //3.粉丝
     * //3.1 查询
     * //3.2 封装vo
     * //3.3 创建pageBeanVo
     * //4.谁看过我
     * //4.1 查询
     * //4.2 封装vo
     * //4.3 创建pageBeanVo
     */
    @Override
    public PageBeanVo findUserList(Integer pageNumber, Integer pageSize, Integer type, Long loginUserId) {
        PageBeanVo pageBeanVo = null;
        int startIndex = (pageNumber - 1) * pageSize;
        List<UserLikeVo> userLikeVoList = new ArrayList<>();
        //明确分页 明确转换的对象
        if(type == 1){
            //1.互相喜欢
            //1.1 查询
            Query query = new Query(
                    Criteria.where("userId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<Friend> friendList = mongoTemplate.find(query, Friend.class);
            //1.2 封装vo
            if(CollectionUtil.isNotEmpty(friendList)){
                for (Friend friend : friendList) {
                    UserLikeVo userLikeVo = new UserLikeVo();
                    userLikeVo.setId( friend.getFriendId() );//赋值朋友的id
                    userLikeVoList.add( userLikeVo );// 放入集合中
                }
            }
            //1.3 创建pageBeanVo
            long count = mongoTemplate.count(query, Friend.class);
            pageBeanVo = new PageBeanVo(pageNumber ,pageSize, count , userLikeVoList);
        }else if(type == 2 ){
            //2.我的喜欢
            //2.1 查询
            Query query = new Query(
                    Criteria.where("userId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
            //2.2 封装vo
            if(CollectionUtil.isNotEmpty(userLikes)){
                for (UserLike userLike : userLikes) {
                    UserLikeVo userLikeVo = new UserLikeVo();
                    userLikeVo.setId( userLike.getLikeUserId() );//我喜欢人的id
                    userLikeVoList.add( userLikeVo );// 放入集合中
                }
            }
            //2.3 创建pageBeanVo
            long count = mongoTemplate.count(query, UserLike.class);
            pageBeanVo = new PageBeanVo(pageNumber ,pageSize, count , userLikeVoList);
        }else if(type ==3 ){
            //3.我的粉丝
            //3.1 查询
            Query query = new Query(
                    Criteria.where("likeUserId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
            //3.2 封装vo
            if(CollectionUtil.isNotEmpty(userLikes)){
                for (UserLike userLike : userLikes) {
                    UserLikeVo userLikeVo = new UserLikeVo();
                    userLikeVo.setId( userLike.getUserId() ); // 谁喜欢我的 这些人id

                    if( isUserLike(loginUserId , userLike.getUserId() )){//是否互相喜欢
                        userLikeVo.setAlreadyLove(true);
                    }

                    userLikeVoList.add(userLikeVo);
                }
            }
            //3.3 创建pageBeanVo
            long count = mongoTemplate.count(query, UserLike.class);
            pageBeanVo = new PageBeanVo(pageNumber , pageSize , count ,userLikeVoList );
        }else if(type == 4 ){
            //4.谁看过我
            //4.1 查询
            Query query = new Query(
                    Criteria.where("userId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
            if(CollectionUtil.isNotEmpty(visitorList)){
                //4.2 封装vo
                for (Visitor visitor : visitorList) {
                    UserLikeVo userLikeVo = new UserLikeVo();
                    userLikeVo.setId(visitor.getVisitorUserId());//设置访客的id



                    userLikeVoList.add(userLikeVo);
                }
            }

            //4.3 创建pageBeanVo
            long count = mongoTemplate.count(query, Visitor.class);
            pageBeanVo = new PageBeanVo(pageNumber , pageSize , count , userLikeVoList);
        }
        return pageBeanVo;
    }

    /**
     * 取消喜欢
     * @param loginUserId
     * @param uid
     */
    @Override
    public void deleteLike(Long loginUserId, Long uid) {
        //1.取消喜欢关系  我不再喜欢
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
                        .and("likeUserId").is(uid)
        );

        mongoTemplate.remove(query, UserLike.class);

    }
}
